﻿@page "/Grid/Grouping/CustomGrouping"
@layout DataProviderAccessArea<INwindDataProvider>

<DemoPageSectionComponent Id="Grid-Grouping-CustomGrouping" ShowSizeMode="true">
    <ChildContentWithParameters Context="Params">
        @inject NwindDataService NwindDataService

        <div class="grid-container">
            <DxGrid @ref="Grid"
                    Data="@Data"
                    ShowGroupPanel="true"
                    CustomGroup="Grid_CustomGroup"
                    CustomizeGroupValueDisplayText="Grid_CustomizeGroupValueDisplayText"
                    ColumnResizeMode="GridColumnResizeMode.NextColumn"
                    TextWrapEnabled="false"
                    SizeMode="Params.SizeMode"
                    VirtualScrollingEnabled="true"
                    AutoExpandAllGroupRows="true">
                <Columns>
                    <DxGridDataColumn FieldName="ProductName" MinWidth="100" />
                    <DxGridDataColumn FieldName="CategoryName" MinWidth="100" />
                    <DxGridDataColumn FieldName="Country" Width="10%" />
                    <DxGridDataColumn FieldName="OrderDate" MinWidth="70" Width="10%" />
                    <DxGridDataColumn FieldName="UnitPrice" DisplayFormat="c" GroupIndex="0" GroupInterval="GridColumnGroupInterval.Custom" Width="10%" />
                    <DxGridDataColumn FieldName="Quantity" Width="10%" />
                </Columns>
                <TotalSummary>
                    <DxGridSummaryItem SummaryType="GridSummaryItemType.Count" FieldName="ProductName" />
                </TotalSummary>
                <GroupSummary>
                    <DxGridSummaryItem SummaryType="GridSummaryItemType.Count" FieldName="UnitPrice" />
                </GroupSummary>
            </DxGrid>
        </div>

        @code {
            object Data { get; set; }
            IGrid Grid { get; set; }

            protected override async Task OnInitializedAsync() {
                var invoices = await NwindDataService.GetInvoicesAsync();
                var products = await NwindDataService.GetProductsAsync();
                var categories = await NwindDataService.GetCategoriesAsync();
                Data = invoices.Join(products, i => i.ProductId, p => p.ProductId, (i, p) => {
                    return new {
                        ProductName = i.ProductName,
                        CategoryId = p.CategoryId,
                        Country = i.Country,
                        OrderDate = i.OrderDate,
                        UnitPrice = i.UnitPrice,
                        Quantity = i.Quantity
                    };
                }).Join(categories, i => i.CategoryId, c => c.CategoryId, (i, c) => {
                    return new {
                        ProductName = i.ProductName,
                        CategoryName = c.CategoryName,
                        Country = i.Country,
                        OrderDate = i.OrderDate,
                        UnitPrice = i.UnitPrice,
                        Quantity = i.Quantity
                    };
                });
            }
            void Grid_CustomGroup(GridCustomGroupEventArgs e) {
                if(e.FieldName == "UnitPrice") {
                    e.SameGroup = Grid_CompareColumnValues(e.Value1, e.Value2) == 0;
                    e.Handled = true;
                }
            }
            int Grid_CompareColumnValues(object value1, object value2) {
                double val1 = Math.Floor(Convert.ToDouble(value1) / 10);
                double val2 = Math.Floor(Convert.ToDouble(value2) / 10);
                var res = System.Collections.Comparer.Default.Compare(val1, val2);
                if(res < 0)
                    res = -1;
                else if(res > 0)
                    res = 1;
                if(res == 0 || (val1 > 9 && val2 > 9))
                    res = 0;
                return res;
            }
            void Grid_CustomizeGroupValueDisplayText(GridCustomizeGroupValueDisplayTextEventArgs e) {
                if(e.FieldName == "UnitPrice") {
                    double val = Math.Floor(Convert.ToDouble(e.Value) / 10);
                    string displayText = string.Format("{0:c} - {1:c} ", val * 10, (val + 1) * 10);
                    if(val > 9)
                        displayText = string.Format(">= {0:c} ", 100);
                    e.DisplayText = displayText;
                }
            }
        }
    </ChildContentWithParameters>
</DemoPageSectionComponent>
